home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap15 / ShowDib1 / ShowDib1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-16  |  6.2 KB  |  195 lines

  1. /*----------------------------------------------
  2.    SHOWDIB1.C -- Shows a DIB in the client area
  3.                  (c) Charles Petzold, 1998
  4.   ----------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "dibfile.h"
  8. #include "resource.h"
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. TCHAR szAppName[] = TEXT ("ShowDib1") ;
  13.  
  14. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  15.                     PSTR szCmdLine, int iCmdShow)
  16. {
  17.      HACCEL   hAccel ;
  18.      HWND     hwnd ;
  19.      MSG      msg ;
  20.      WNDCLASS wndclass ;
  21.  
  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.      wndclass.lpfnWndProc   = WndProc ;
  24.      wndclass.cbClsExtra    = 0 ;
  25.      wndclass.cbWndExtra    = 0 ;
  26.      wndclass.hInstance     = hInstance ;
  27.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  30.      wndclass.lpszMenuName  = szAppName ;
  31.      wndclass.lpszClassName = szAppName ;
  32.  
  33.      if (!RegisterClass (&wndclass))
  34.      {
  35.           MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
  36.                       szAppName, MB_ICONERROR) ;
  37.           return 0 ;
  38.      }
  39.  
  40.      hwnd = CreateWindow (szAppName, TEXT ("Show DIB #1"),
  41.                           WS_OVERLAPPEDWINDOW,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           CW_USEDEFAULT, CW_USEDEFAULT, 
  44.                           NULL, NULL, hInstance, NULL) ;
  45.  
  46.      ShowWindow (hwnd, iCmdShow) ;
  47.      UpdateWindow (hwnd) ;
  48.  
  49.      hAccel = LoadAccelerators (hInstance, szAppName) ;
  50.  
  51.      while (GetMessage (&msg, NULL, 0, 0))
  52.      {
  53.           if (!TranslateAccelerator (hwnd, hAccel, &msg))
  54.           {
  55.                TranslateMessage (&msg) ;
  56.                DispatchMessage (&msg) ;
  57.           }
  58.      }
  59.      return msg.wParam ;
  60. }
  61.  
  62. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  63. {
  64.      static BITMAPFILEHEADER * pbmfh ;
  65.      static BITMAPINFO       * pbmi ;
  66.      static BYTE             * pBits ;
  67.      static int                cxClient, cyClient, cxDib, cyDib ;
  68.      static TCHAR              szFileName [MAX_PATH], szTitleName [MAX_PATH] ;
  69.      BOOL                      bSuccess ;
  70.      HDC                       hdc ;
  71.      PAINTSTRUCT               ps ;
  72.  
  73.      switch (message)
  74.      {
  75.      case WM_CREATE:
  76.           DibFileInitialize (hwnd) ;
  77.           return 0 ;
  78.  
  79.      case WM_SIZE:
  80.           cxClient = LOWORD (lParam) ;
  81.           cyClient = HIWORD (lParam) ;
  82.           return 0 ;
  83.  
  84.      case WM_INITMENUPOPUP:
  85.           EnableMenuItem ((HMENU) wParam, IDM_FILE_SAVE,   
  86.                           pbmfh ? MF_ENABLED : MF_GRAYED) ;
  87.           return 0 ;
  88.  
  89.      case WM_COMMAND:
  90.           switch (LOWORD (wParam))
  91.           {
  92.           case IDM_FILE_OPEN:
  93.                     // Show the File Open dialog box
  94.  
  95.                if (!DibFileOpenDlg (hwnd, szFileName, szTitleName))
  96.                     return 0 ;
  97.                
  98.                     // If there's an existing DIB, free the memory
  99.  
  100.                if (pbmfh)
  101.                {
  102.                     free (pbmfh) ;
  103.                     pbmfh = NULL ;
  104.                }
  105.                     // Load the entire DIB into memory
  106.  
  107.                SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
  108.                ShowCursor (TRUE) ;
  109.  
  110.                pbmfh = DibLoadImage (szFileName) ;
  111.  
  112.                ShowCursor (FALSE) ;
  113.                SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
  114.  
  115.                     // Invalidate the client area for later update
  116.  
  117.                InvalidateRect (hwnd, NULL, TRUE) ;
  118.  
  119.                if (pbmfh == NULL)
  120.                {
  121.                     MessageBox (hwnd, TEXT ("Cannot load DIB file"), 
  122.                                 szAppName, 0) ;
  123.                     return 0 ;
  124.                }
  125.                     // Get pointers to the info structure & the bits
  126.  
  127.                pbmi  = (BITMAPINFO *) (pbmfh + 1) ;
  128.                pBits = (BYTE *) pbmfh + pbmfh->bfOffBits ;
  129.  
  130.                     // Get the DIB width and height
  131.  
  132.                if (pbmi->bmiHeader.biSize == sizeof (BITMAPCOREHEADER))
  133.                {
  134.                     cxDib = ((BITMAPCOREHEADER *) pbmi)->bcWidth ;
  135.                     cyDib = ((BITMAPCOREHEADER *) pbmi)->bcHeight ;
  136.                }
  137.                else
  138.                {
  139.                     cxDib =      pbmi->bmiHeader.biWidth ;
  140.                     cyDib = abs (pbmi->bmiHeader.biHeight) ;
  141.                }
  142.                return 0 ;
  143.  
  144.           case IDM_FILE_SAVE:
  145.                     // Show the File Save dialog box
  146.  
  147.                if (!DibFileSaveDlg (hwnd, szFileName, szTitleName))
  148.                     return 0 ;
  149.                
  150.                     // Save the DIB to memory
  151.  
  152.                SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
  153.                ShowCursor (TRUE) ;
  154.  
  155.                bSuccess = DibSaveImage (szFileName, pbmfh) ;
  156.  
  157.                ShowCursor (FALSE) ;
  158.                SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
  159.  
  160.                if (!bSuccess)
  161.                     MessageBox (hwnd, TEXT ("Cannot save DIB file"), 
  162.                                 szAppName, 0) ;
  163.                return 0 ;
  164.           }
  165.           break ;
  166.  
  167.      case WM_PAINT:
  168.           hdc = BeginPaint (hwnd, &ps) ;
  169.  
  170.           if (pbmfh)
  171.                SetDIBitsToDevice (hdc, 
  172.                                   0,         // xDst
  173.                                   0,         // yDst
  174.                                   cxDib,     // cxSrc
  175.                                   cyDib,     // cySrc
  176.                                   0,         // xSrc
  177.                                   0,         // ySrc
  178.                                   0,         // first scan line
  179.                                   cyDib,     // number of scan lines
  180.                                   pBits, 
  181.                                   pbmi, 
  182.                                   DIB_RGB_COLORS) ;
  183.  
  184.           EndPaint (hwnd, &ps) ;
  185.           return 0 ;
  186.           
  187.      case WM_DESTROY:
  188.           if (pbmfh)
  189.                free (pbmfh) ;
  190.  
  191.           PostQuitMessage (0) ;
  192.           return 0 ;
  193.      }
  194.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  195. }